package b.i.d;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.CancellationSignal;
import android.util.Log;
import b.f.h;
import b.i.c.b.c;
import b.i.c.b.d;
import b.i.h.e;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.util.List;

/* compiled from: TypefaceCompatApi24Impl */
public class f extends k {
    public static final Class<?> b;
    public static final Constructor<?> c;
    public static final Method d;
    public static final Method e;

    static {
        Class cls;
        Method method;
        Method method2;
        Constructor constructor = null;
        try {
            cls = Class.forName("android.graphics.FontFamily");
            Constructor constructor2 = cls.getConstructor(new Class[0]);
            r5 = new Class[5];
            Class cls2 = Integer.TYPE;
            r5[1] = cls2;
            r5[2] = List.class;
            r5[3] = cls2;
            r5[4] = Boolean.TYPE;
            method = cls.getMethod("addFontWeightStyle", r5);
            constructor = Typeface.class.getMethod("createFromFamiliesWithDefault", new Class[]{Array.newInstance(cls, 1).getClass()});
            method2 = constructor;
            constructor = constructor2;
        } catch (ClassNotFoundException | NoSuchMethodException e) {
            Log.e("TypefaceCompatApi24Impl", e.getClass().getName(), e);
            cls = constructor;
            method2 = cls;
            method = method2;
        }
        c = constructor;
        b = cls;
        d = method;
        e = method2;
    }

    public static boolean f(Object obj, ByteBuffer byteBuffer, int i, int i2, boolean z) {
        try {
            return ((Boolean) d.invoke(obj, new Object[]{byteBuffer, Integer.valueOf(i), null, Integer.valueOf(i2), Boolean.valueOf(z)})).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return false;
        }
    }

    public static Typeface g(Object obj) {
        try {
            Array.set(Array.newInstance(b, 1), 0, obj);
            return (Typeface) e.invoke(null, new Object[]{r1});
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return null;
        }
    }

    public Typeface a(Context context, c cVar, Resources resources, int i) {
        Object newInstance;
        Throwable th;
        try {
            newInstance = c.newInstance(new Object[0]);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException unused) {
            newInstance = null;
        }
        if (newInstance == null) {
            return null;
        }
        for (d dVar : cVar.a) {
            ByteBuffer map;
            int i2 = dVar.f;
            File z0 = b.i.b.f.z0(context);
            Resources resources2 = resources;
            if (z0 != null) {
                try {
                    if (b.i.b.f.b0(z0, resources2, i2)) {
                        FileInputStream fileInputStream;
                        try {
                            fileInputStream = new FileInputStream(z0);
                            FileChannel channel = fileInputStream.getChannel();
                            map = channel.map(MapMode.READ_ONLY, 0, channel.size());
                            fileInputStream.close();
                        } catch (IOException unused2) {
                            map = null;
                        } catch (Throwable th2) {
                            th.addSuppressed(th2);
                        }
                        z0.delete();
                        if (map != null || !f(newInstance, map, dVar.e, dVar.b, dVar.c)) {
                            return null;
                        }
                    }
                } finally {
                    z0.delete();
                }
            }
            map = null;
            if (map != null) {
                return null;
            }
        }
        return g(newInstance);
    }

    public Typeface b(Context context, CancellationSignal cancellationSignal, e.c[] cVarArr, int i) {
        Object newInstance;
        int i2 = 0;
        try {
            newInstance = c.newInstance(new Object[0]);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException unused) {
            newInstance = null;
        }
        if (newInstance == null) {
            return null;
        }
        h hVar = new h();
        int length = cVarArr.length;
        while (i2 < length) {
            e.c cVar = cVarArr[i2];
            Uri uri = cVar.a;
            ByteBuffer byteBuffer = (ByteBuffer) hVar.get(uri);
            if (byteBuffer == null) {
                byteBuffer = b.i.b.f.F0(context, cancellationSignal, uri);
                hVar.put(uri, byteBuffer);
            }
            if (byteBuffer == null || !f(newInstance, byteBuffer, cVar.b, cVar.c, cVar.d)) {
                return null;
            }
            i2++;
        }
        Typeface g = g(newInstance);
        if (g == null) {
            return null;
        }
        return Typeface.create(g, i);
    }
}
